package kgc.crm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kgc.crm.mapper.RoleMapper;
import kgc.crm.mapper.UserMapper;
import kgc.crm.mapper.UserRoleMapper;
import kgc.crm.model.Menu;
import kgc.crm.model.Role;
import kgc.crm.model.User;
import kgc.crm.model.UserRole;
import kgc.crm.service.IRoleService;
import kgc.crm.util.EmptyChecker;
import kgc.crm.util.RequestUtil;
import kgc.crm.util.ResultUtil;
import kgc.crm.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl  implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public ResponseEntity list() {
        //查看全部角色   创建wrapper 设置不隐藏
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq(Role.COL_IS_HIDDEN,1);
        List<Role> roles = roleMapper.selectList(wrapper);
        return ResultUtil.success(roles);
    }

    @Override
    public ResponseEntity add(Role role) {
        if (EmptyChecker.isEmpty(role)) {
            log.error("【角色模块】添加失败，role对象为空",role);
            return ResultUtil.error("添加失败");
        }
        //判断实体类中的所有属性不能为空
        if (!EmptyChecker.mybatisIsNull(role)) {
            log.error("【角色模块】添加失败，role对象中不能为空",role);
            return ResultUtil.error("添加失败");
        }
        role.setRemark("添加了"+role.getRoleName()+"用户");
        role.setRoleType(0);
        int insert =-1;
        try {
            insert = roleMapper.insert(role);
        } catch (Exception e) {
            log.error("【角色模块】添加失败，sql语句执行失败",e);
            return ResultUtil.error("添加失败");
        }
        if (insert <= 0) {
            log.error("【角色模块】添加失败，sql语句执行失败",insert);
            return ResultUtil.error("添加失败");
        }
        return ResultUtil.success("添加成功");
    }

    /**
     *  根据角色Id查询该角色下的所有用户
     * @param vo 返回的类型
     * @return
     */
    @Override
    public ResponseEntity listByRoleId(RoleVo vo) {
        if (EmptyChecker.isEmpty(vo)) {
            log.error("【角色模块】角色查询失败，vo不能为空",vo);
            return ResultUtil.error("查询失败");
        }
        if (EmptyChecker.isEmpty(vo.getRoleId())) {
            log.error("【角色模块】角色查询失败，roleId不能为空",vo.getRoleId());
            return ResultUtil.error("查询失败");
        }
        //判断当前页如果等于null或者小于等于0的话设置当前页为1
        if (vo.getPageIndex() == null || vo.getPageIndex()<=0) {
            vo.setPageIndex(1);
        }
        if (vo.getPageSize()==null || vo.getPageSize() <= 0) {
            vo.setPageSize(10);
        }
        List<Role> roles = roleMapper.selectListByRoleId((vo.getPageIndex()-1)*vo.getPageSize(), vo.getPageSize(), vo.getRoleId());
        //分页  selectListByRoleIdCount获取总条数
        Page <Role> page= new Page<>(vo.getPageIndex(),vo.getPageSize(),roleMapper.selectListByRoleIdCount(vo.getRoleId()));
        page.setRecords(roles);//进行分页操作

        return ResultUtil.success(page);
    }


    @Override
    public ResponseEntity upd(Role role) {
        if (EmptyChecker.isEmpty(role)) {
            log.error("【角色模块】修改失败，role对象为空",role);
            return ResultUtil.error("修改失败");
        }
        if (EmptyChecker.isEmpty(role.getRoleId())) {
            log.error("【角色模块】修改失败，roleId中不能为空",role.getRoleId());
            return ResultUtil.error("修改失败");
        }
        int update =-1;
        try {
            update = roleMapper.updateById(role);
        } catch (Exception e) {
            log.error("【角色模块】修改失败，sql语句执行失败",e);
            return ResultUtil.error("修改失败");
        }
        if (update <= 0) {
            log.error("【角色模块】修改失败，sql语句执行失败",update);
            return ResultUtil.error("修改失败");
        }
        return ResultUtil.success("修改成功");
    }

    @Override
    public ResponseEntity del(Role role) {
        if (EmptyChecker.isEmpty(role)) {
            log.error("【角色模块】删除失败，role对象为空",role);
            return ResultUtil.error("删除失败");
        }
        if (EmptyChecker.isEmpty(role.getRoleId())) {
            log.error("【角色模块】删除失败，roleId中不能为空",role.getRoleId());
            return ResultUtil.error("删除失败");
        }
        int delete =-1;
        try {
            delete = roleMapper.deleteById(role);
        } catch (Exception e) {
            log.error("【角色模块】删除失败，sql语句执行失败",e);
            return ResultUtil.error("删除失败");
        }
        if (delete <= 0) {
            log.error("【角色模块】删除失败，sql语句执行失败",delete);
            return ResultUtil.error("删除失败");
        }
        return ResultUtil.success("删除成功");
    }

    @Override
    public ResponseEntity copy(Role role) {
     //   Role role = roleMapper.selectById(role.getRoleId());
        //进来直接调用根据id查询角色方法,获取到一个对象
        //判断如果角色已经被删除的话就copy不了
//        if (id.getStatus() == 0) {
//            log.error("【角色模块】复制失败，角色已经被删除",role.getStatus());
//            return ResultUtil.error("复制失败");
//        }
        //这边因为status加了逻辑删除，所以==0的话会报空指针异常
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq(Role.COL_ROLE_ID,role.getRoleId());
        Role role1 = roleMapper.selectById(role.getRoleId());
        //roleMapper.selectByMap()
        try {
            if (role1 != null) {
                QueryWrapper<Role> wrapper = new QueryWrapper<>();
                wrapper.likeRight(Role.COL_ROLE_NAME,role1.getRoleName());
                Long aLong = roleMapper.selectCount(wrapper);
                //查询总条数
                role.setRoleId(null);
                //把id设为空
                role.setRoleName(role1.getRoleName()+"("+aLong+")");
                return add(role);
            }else
                return ResultUtil.error("删除失败，用户已经被删除");
        } catch (Exception e) {
            log.error("【角色模块】复制失败，角色已经被删除,", e);
            return ResultUtil.error("e复制失败");
        }

        //然后调用新增的方法 把已经设置好属性的role对象传进去进行copy
    }
}
